/**
 * 
 */
package spl.army.ut;

import static org.junit.Assert.*;

import java.security.acl.LastOwnerException;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import spl.army.ItemRequired;
import spl.army.Mission;
import spl.army.Sergeant;

/**
 * @author Ruby
 *
 */
public class MissionTest {
	private Mission mission;
	private String name = "KillBinLaden";
	private String skill = "Sniper";
	private int timeLeft = 5;
	private ArrayList<ItemRequired> itemsRequireds = new ArrayList<ItemRequired>();
	private ArrayList<Mission> preReq= new ArrayList<Mission>();
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		
		this.name = "KillBinLaden";
		this.skill = "Sniper";
		this.timeLeft = 5;
		this.itemsRequireds = new ArrayList<ItemRequired>();
		this.preReq= new ArrayList<Mission>();		
		this.mission = new Mission(
							this.name,
							this.skill,
							this.timeLeft,
							this.itemsRequireds,
							this.preReq);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	/**
	 * Test method for {@link spl.army.Mission#assignSergeant(spl.army.Sergeant)}.
	 */
	@Test
	public void testAssignSergeant() {
		try{
			this.mission.getAssignedSergeant();
		}catch(IllegalStateException e)
		{
			Sergeant sergeant = new Sergeant("moshe", "5", "sniper", "6", "shortestMission", "10");
			try {
				this.mission.assignSergeant(sergeant);
				assertNotNull(this.mission.getAssignedSergeant());
			} catch (LastOwnerException e1) {
				fail("Shouldn't throw exception now");
			}
			try {
				Sergeant sergeant2 = new Sergeant("moshe2", "5", "sniper", "6", "shortestMission", "10");
				this.mission.assignSergeant(sergeant2);
			} catch (LastOwnerException e1) {
				return;
			}
			fail("Should throw LastOwnerException");
		}
		fail("Should throw IllegalStateException");
	}

	/**
	 * Test method for {@link spl.army.Mission#setActive()}.
	 */
	@Test
	public void testActiveness() {
		assertFalse(this.mission.isActive());
		this.mission.setActive();
		assertTrue(this.mission.isActive());
		this.mission.disableActive();
		assertFalse(this.mission.isActive());
	}

	/**
	 * Test method for {@link spl.army.Mission#getMissionName()}.
	 */
	@Test
	public void testGetMissionName() {
		assertEquals(this.name, this.mission.getMissionName());
	}

	/**
	 * Test method for {@link spl.army.Mission#getRequiredSkill()}.
	 */
	@Test
	public void testGetRequiredSkill() {
		assertEquals(this.skill, this.mission.getRequiredSkill());
	}

	/**
	 * Test method for {@link spl.army.Mission#getTimeLeft()}.
	 */
	@Test
	public void testGetTimeLeft() {
		assertEquals(this.timeLeft, this.mission.getTimeLeft());
	}

	/**
	 * Test method for {@link spl.army.Mission#setTimeLeft(int)}.
	 */
	@Test
	public void testSetTimeLeft() {
		assertEquals(this.timeLeft, this.mission.getTimeLeft());
		this.mission.setTimeLeft(3);
		assertEquals(3, this.mission.getTimeLeft());
	}

	/**
	 * Test method for {@link spl.army.Mission#getItemsRequired()}.
	 */
	@Test
	public void testGetItemsRequired() {
		List<ItemRequired> reqItems = this.mission.getItemsRequired();
		assertTrue(reqItems.size() == 0);
		ItemRequired axe = new ItemRequired("AXE", 3);
		this.itemsRequireds.add(axe);
		this.mission = new Mission(
							this.name,
							this.skill,
							this.timeLeft,
							this.itemsRequireds,
							this.preReq);
		reqItems = this.mission.getItemsRequired();
		assertTrue(reqItems.size() == 1);
		assertTrue(reqItems.get(0) == axe);
	}

	/**
	 * Test method for {@link spl.army.Mission#getPreReqMissions()}.
	 */
	@Test
	public void testGetPreReqMissions() {
		List<Mission> reqMissions = this.mission.getPreReqMissions();
		assertTrue(reqMissions.size() == 0);
		ArrayList<Mission> binladenPre = new ArrayList<Mission>();
		Mission binladen = new Mission("binladen", "sniper", 5, this.itemsRequireds, binladenPre );
		this.preReq.add(binladen);
		this.mission = new Mission(
							this.name,
							this.skill,
							this.timeLeft,
							this.itemsRequireds,
							this.preReq);
		reqMissions = this.mission.getPreReqMissions();
		assertTrue(reqMissions.size() == 1);
		assertTrue(reqMissions.get(0) == binladen);
	}

	/**
	 * Test method for {@link spl.army.Mission#updateTimeLeft(int)}.
	 */
	@Test
	public void testUpdateTimeLeft() {
		this.mission.setTimeLeft(0);
		assertEquals(this.mission.getTimeLeft(), 0);
	}

	/**
	 * Test method for {@link spl.army.Mission#addPreRequest(spl.army.Mission)}.
	 */
	@Test
	public void testAddPreRequestMission() {
		List<Mission> reqMissions = this.mission.getPreReqMissions();
		int originalSize = reqMissions.size();
		ArrayList<Mission> binladenPre = new ArrayList<Mission>();
		Mission binladen = new Mission("binladen", "sniper", 5, this.itemsRequireds, binladenPre );
		this.mission.addPreRequest(binladen);
		reqMissions = this.mission.getPreReqMissions();
		assertEquals(reqMissions.size(), originalSize+1);
	}

	/**
	 * Test method for {@link spl.army.Mission#addPreRequest(java.util.List)}.
	 */
	@Test
	public void testAddPreRequestListOfMission() {
		List<Mission> reqMissions = this.mission.getPreReqMissions();
		int originalSize = reqMissions.size();
		ArrayList<Mission> emptyPre = new ArrayList<Mission>();
		ArrayList<Mission> missionsToAdd = new ArrayList<Mission>();
		missionsToAdd.add(new Mission("binladen", "sniper", 5, this.itemsRequireds, emptyPre));
		missionsToAdd.add(new Mission("binladen2", "sniper", 5, this.itemsRequireds, emptyPre));
		missionsToAdd.add(new Mission("binladen3", "sniper", 5, this.itemsRequireds, emptyPre));
		this.mission.addPreRequest(missionsToAdd);
		reqMissions = this.mission.getPreReqMissions();
		assertEquals(reqMissions.size(), originalSize+3);
		Mission currentMission;
		for(int i=0;i<missionsToAdd.size();i++){
			 currentMission = missionsToAdd.get(i);
			 assertEquals(currentMission, reqMissions.get(originalSize+i));
		}
	}

	/**
	 * Test method for {@link spl.army.Mission#checkPrerequest()}.
	 */
	@Test
	public void testCheckPrerequest() {
		assertTrue(this.mission.checkPrerequest());
		ArrayList<Mission> binladenPre = new ArrayList<Mission>();
		Mission binladen = new Mission("binladen", "sniper", 5, this.itemsRequireds, binladenPre );
		this.preReq.add(binladen);
		this.mission = new Mission(
							this.name,
							this.skill,
							this.timeLeft,
							this.itemsRequireds,
							this.preReq);
		assertFalse(this.mission.checkPrerequest());
		binladen.setTimeLeft(0);
		assertTrue(this.mission.checkPrerequest());
	}

}
